package cn.shenlong.portal.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.shenlong.common.Constants;
import cn.shenlong.util.UploadResponse;
import cn.shenlong.util.UploadUtil;

@Controller
@RequestMapping("/ueditorc")
public class UeditorController {

	
	@RequestMapping("/upload" )
	public void upload(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		try {
			List<Map<String, Object>> resultList = UploadUtil.uploadImage(
					request, Constants.UEDITOR_IMG_URI);
			UploadUtil.showImageForUeditor(resultList, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
 
	
	
	
	@RequestMapping(value ="fckupload" )
	public void uploadAttachment(
			@RequestParam(value = "NewFile", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		String path = null;
		try {
		 
			List<Map<String, Object>> resultList = UploadUtil.uploadImage(
					request, Constants.UEDITOR_IMG_URI);
			path = (String)resultList.get(0).get("imagePath");
		} catch (Exception e) {

			PrintWriter out;
			try {
				out = response.getWriter();
				out.print(UploadResponse.getFileUploadWriteError(request));
				out.flush();
				out.close();
				return;
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				return;
			}

		}
		
		PrintWriter out;
		try {
			out = response.getWriter();
			  //设置返回“图像”选项卡  
			 String callback = request.getParameter("CKEditorFuncNum");    
			 out.println("<script type=\"text/javascript\">");    
			 out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + path+ "','')");    
			 out.println("</script>"); 

			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
}
